package com.dai.common;

import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

/**
 * @author daijiyong
 */
public class Node {
    public Integer value;
    public Node next;

    public Node() {
    }

    public Node(Integer value) {
        this.value = value;
    }

    public Node(Integer value, Node next) {
        this.value = value;
        this.next = next;
    }


    public String toStringWhenCircle() {
        Node temp = this;
        StringBuilder str = new StringBuilder();
        Set<Integer> set = new HashSet<>();
        while (temp != null && !set.contains(temp.value)) {
            str.append(temp.value).append("->");
            set.add(temp.value);
            temp = temp.next;
        }
        if (temp != null) {
            str.append(temp.value);
        }
        return str.toString();
    }

    @Override
    public String toString() {
        Node temp = this;
        StringBuilder str = new StringBuilder();
        while (temp != null) {
            str.append(temp.value).append("->");
            temp = temp.next;
        }
        str.append("null");
        return str.toString();
    }
}
